package com.hsylient.javase.chapter01;

import com.hsylient.javase.common.entity.Student;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;

import java.time.LocalDate;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
public class ArraysTest {
    @Test
    public void testField() {
        // NONE
    }

    @Test
    public void testQuickSort() {
        Integer[] array = {2, 1, 9, 5, 7, 7, 8, 2, 9, 0, 2, 6};
        Arrays.sort(array);
        log.info("[测试快排] 排序结果 {}", Arrays.toString(array));
    }

    @Test
    public void testQuickSortByRange() {
        Integer[] array = {2, 1, 9, 5, 7, 7, 8, 2, 9, 0, 2, 6};
        Arrays.sort(array, 0, 3);
        log.info("[测试数组局部排序] 排序结果 {}", Arrays.toString(array));
    }

    @Test
    public void testParallelSort() {
        Integer[] array = {2, 1, 9, 5, 7, 7, 8, 2, 9, 0, 2, 6};
        Arrays.parallelSort(array, Comparator.comparingInt(x -> x));
        log.info("[测试并行排序] 排序结果 {}", Arrays.toString(array));
    }

    @Test
    public void testThenComparing() {
        Student zhangXin = new Student("张鑫", 11, LocalDate.now());
        Student liHua = new Student("李华", 12, LocalDate.now().plusDays(12));
        Student sunWuKong = new Student("孙悟空", 12, LocalDate.now().plusDays(13));
        Student zhuBaJie = new Student("猪八戒", 13, LocalDate.now().plusDays(-11));
        Student shaHeShang = new Student("沙和尚", 14, LocalDate.now());
        Student neZha = new Student("哪吒", 14, LocalDate.now().plusDays(-1));
        // 组装LIST
        List<Student> studentList = Arrays.asList(zhangXin, liHua, sunWuKong, zhuBaJie, shaHeShang, neZha);
        // 按照age倒叙排列，age一样按照按照生日排序，生日一样按照姓名排序然后整体反转
        studentList.stream()
                .sorted(
                        Comparator.comparing(Student::getAge, Comparator.reverseOrder())
                                .thenComparing(Student::getBirthday, LocalDate::compareTo)
                                .thenComparing(Student::getName)
                                .reversed()
                ).collect(Collectors.toList())
                .forEach(System.out::println);
    }
}
